// src/mock/modules/case.js
import Mock from 'mockjs'
import { successResponse, errorResponse } from '@/mock/utils'

// 生成案件数据
const generateCases = Mock.mock({
    'list|50': [
        {
            'id|+1': 1000,
            'caseName': '@ctitle(5,10)',
            'status|1-7': 1,
            'caseType|1-4': 1,
            'createTime': '@datetime("yyyy-MM-dd")',
            'realName': '@cname',
            'lawyerName': '@cname',
            'description': '@cparagraph(2,5)'
        }
    ]
}).list

export default function(mock) {
    // 获取案件列表
    mock.onGet(/api\/cases/).reply(config => {
        const params = config.params || {}
        const page = Number(params.page) || 1
        const size = Number(params.size) || 10
        const { caseName, status, caseType, dateRange } = params

        console.log('[Mock] 处理案件列表请求，参数:', params)

        try {
            // 过滤数据
            let filtered = [...generateCases]

            if (caseName) {
                filtered = filtered.filter(item =>
                    item.caseName.toLowerCase().includes(caseName.toLowerCase())
                )
            }

            if (status) {
                filtered = filtered.filter(item =>
                    String(item.status) === String(status)
                )
            }

            if (caseType) {
                filtered = filtered.filter(item =>
                    String(item.caseType) === String(caseType)
                )
            }

            if (dateRange && dateRange.length === 2) {
                const [start, end] = dateRange
                filtered = filtered.filter(item => {
                    const itemDate = new Date(item.createTime)
                    const startDate = new Date(start)
                    const endDate = new Date(end)
                    return itemDate >= startDate && itemDate <= endDate
                })
            }

            // 分页处理
            const total = filtered.length
            const startIndex = (page - 1) * size
            const endIndex = startIndex + size
            const data = filtered.slice(startIndex, endIndex)

            return [200, successResponse({
                list: data,
                pagination: {
                    page,
                    size,
                    total
                }
            })]
        } catch (error) {
            console.error('[Mock] 处理案件列表错误:', error)
            return [500, errorResponse('服务器内部错误')]
        }
    })

    // 获取案件详情
    mock.onGet(/api\/cases\/\d+/).reply(config => {
        const id = config.url.split('/').pop()
        const caseData = generateCases.find(item => item.id == id)

        if (!caseData) {
            return [404, errorResponse('案件不存在')]
        }

        return [200, successResponse(caseData)]
    })

    // 更新案件
    mock.onPut(/api\/cases\/\d+/).reply(config => {
        const id = config.url.split('/').pop()
        const data = JSON.parse(config.data)
        const index = generateCases.findIndex(item => item.id == id)

        if (index === -1) {
            return [404, errorResponse('案件不存在')]
        }

        generateCases[index] = {
            ...generateCases[index],
            ...data
        }

        return [200, successResponse(generateCases[index], '更新成功')]
    })

    // 删除案件
    mock.onDelete(/api\/cases\/\d+/).reply(config => {
        const id = config.url.split('/').pop()
        const index = generateCases.findIndex(item => item.id == id)

        if (index === -1) {
            return [404, errorResponse('案件不存在')]
        }

        generateCases.splice(index, 1)
        return [200, successResponse(null, '删除成功')]
    })
}